package com.yhd.controller;

import com.yhd.entity.dto.*;
import com.yhd.entity.query.CourseAboutVO;
import com.yhd.entity.query.SystemDictionaryVO;
import com.yhd.entity.query.UserVO;
import com.yhd.entity.result.ResultVo;
import com.yhd.model.ResultPage;
import com.yhd.service.CorporateService;
import com.yhd.service.CourseService;
import com.yhd.service.SysDictService;
import com.yhd.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@Api(value = "个人中心")
@Slf4j
@RequestMapping(value = "/app/user")
@CrossOrigin
public class AppUserCenterController extends WebBaseController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private CourseService courseService;
	@Autowired
	private CorporateService corporateService;
	@Autowired
	private SysDictService sysDictService;
	
	
	@ApiOperation(value = "我的约课", httpMethod = "GET",notes = "")
	@ApiImplicitParams({@ApiImplicitParam(name = "status", value = "状态 0=取消，1=正常状态，2=已结束", required = true, paramType = "字符串"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, paramType = "int")})
	@GetMapping("/about")
	@ResponseBody
	public ResultVo<ResultPage<List<CourseAboutDto>>> aboutList(Integer status, Integer pageSize, Integer pageNum) {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		ResultPage<List<CourseAboutDto>> result = courseService.userAboutList(getUserInfo(request), status, pageSize, pageNum);
		return ResultVo.success(result);
	}
	
	@ApiOperation(value = "取消约课", httpMethod = "POST", notes = "{\n" +
			"  \"id\": \"约课id\"\",\n" +
			"  \"status\": \"状态：0=取消\"\n" +
			"}")
	@PostMapping("/cancelAbout")
	@ResponseBody
	public ResultVo cancelAbout(@RequestBody CourseAboutVO aboutVO) throws Exception {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		ResultVo resultVo= courseService.cancelAbout(aboutVO, getUserInfo(request));
		return resultVo;
	}
	
	
	@ApiOperation(value = "我的收藏", httpMethod = "GET",notes = "")
	@GetMapping("/collectedList")
	@ApiImplicitParams(@ApiImplicitParam(name = "type", value = "类型 activity:活动，course:课程", required = true, paramType = "字符串"))
	@ResponseBody
	public ResultVo<List<CourseDto>> collectedList(String type) {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		List<CourseDto> result = courseService.userCollectedList(type, getUserInfo(request).getId());
		return ResultVo.success(result);
	}
	
	@ApiOperation(value = "活动/课程订单", httpMethod = "GET",notes = "")
	@ApiImplicitParams({@ApiImplicitParam(name = "type", value = "类型 activity:活动，course:课程", required = true, paramType = "字符串"),
			@ApiImplicitParam(name = "status", value = "状态：0:待审核 1:待付款  2:已付款 3:拒绝报名 4=已取消", required = false, paramType = "int"),
			@ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "int"),
			@ApiImplicitParam(name = "pageSize", value = "每页显示数量", required = true, paramType = "int")})
	@GetMapping("/orderList")
	@ResponseBody
	public ResultVo<ResultPage<List<CourseBookedDto>>> orderList(String type, Integer status, Integer pageSize, Integer pageNum) {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		ResultPage<List<CourseBookedDto>> result = courseService.userOrderList(type, status, getUserInfo(request).getId(), pageSize, pageNum);
		return ResultVo.success(result);
	}
	
	@ApiOperation(value = "修改用户信息", httpMethod = "POST", notes = "{\n" +
			"  \"head\": \"头像\"\",\n" +
			"  \"area\": \"地区\"\",\n" +
			"  \"sex\": \"性别 0女 1男\"\",\n" +
			"  \"userName\": \"用户名\"\",\n" +
			"  \"cardType\": \"证件类型\"\",\n" +
			"  \"cardNo\": \"证件号码\"\",\n" +
			"  \"birthday\": \"生日\"\",\n" +
			"  \"phone\": \"电话\"\",\n" +
			"  \"company\": \"公司\"\",\n" +
			"  \"referrerUser\": \"职位\"\n" +
			"}")
	@PostMapping("/update")
	@ResponseBody
	public ResultVo<UserDto> updateUser(@RequestBody UserVO userVO) throws Exception {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		ResultVo<UserDto> resultVo = userService.updateUser(userVO,getUserInfo(request));
		return resultVo;
	}
	
	@ApiOperation(value = "关于我们", httpMethod = "GET",notes = "")
	@GetMapping("/ourInfo")
	@ResponseBody
	public ResultVo<TCorporateDto> ourInfo() {
		TCorporateDto result = corporateService.ourInfo();
		return ResultVo.success(result);
	}
	
	@ApiOperation(value = "协议", httpMethod = "GET",notes = "")
	@ApiImplicitParams({@ApiImplicitParam(name = "groupId", value = "300课程协议，400活动协议", required = true, paramType = "int")})
	@GetMapping("/agreement")
	@ResponseBody
	public ResultVo<SystemDictionaryDto> ourInfo(SystemDictionaryVO systemDictionaryVO) {
		SystemDictionaryDto result = sysDictService.selectSysDictInfoByGroupId(systemDictionaryVO);
		return ResultVo.success(result);
	}
	
	@ApiOperation(value = "权益", httpMethod = "GET",notes = "")
	@GetMapping("/rights")
	@ResponseBody
	public ResultVo<List<CourseDto>> rights() {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		List<CourseDto> result = courseService.userRights(getUserInfo(request));
		return ResultVo.success(result);
	}
	
	@ApiOperation(value = "用户信息", httpMethod = "GET",notes = "")
	@GetMapping("/userInfo")
	@ResponseBody
	public ResultVo<UserDto> userInfo() {
		if (getUserInfo(request) == null) {
			return ResultVo.failure("10001","无访问权限，请先登录！");
		}
		UserDto result = userService.selectUserById(getUserInfo(request).getId());
		return ResultVo.success(result);
	}
	
}
